package com.situ.fly.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.ibatis.javassist.expr.NewArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.fly.entity.Answer;
import com.situ.fly.entity.Question;
import com.situ.fly.entity.User;
import com.situ.fly.service.AnswerService;
import com.situ.fly.service.QuestionService;
import com.situ.fly.service.UserService;

@Controller
@RequestMapping("/question/")
public class QuestionController {

	/*
	 * 耦合
	 * 模块与模块之间的关系 
	 *  类与类			具体的代码
	 *  	继承:  最强的类与类之间的关系. 
	 *  		在继承关系下, 父类的修改会影响子类的行为
	 *  
	 *  	关联:  一个类的属性, 是引用了另外一个类, 合成, 聚合
	 *  
	 *  	依赖:  在一个类中使用了另外一个类, 作为参数, 作为局部变量, 作为方法的返回值
	 *  
	 *  类与接口
	 *  接口与接口		抽象的类型, 没有代码的实现
	 *  
	 */
	
	@Autowired
	private QuestionService questionService;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private AnswerService answerService;
	
	@GetMapping("/index")
	public String index() {
		return "/jie/index";
	}
	
	/**
	 * 发表新帖的页面
	 */
	@GetMapping("/add")
	public String add(Model model) {
		model.addAttribute("question", new Question());
		return "/jie/add";
	}
	
	/**
	 * 处理发表新帖的请求
	 */
	@PostMapping("/add")
	public String add(Question question, String vercode,
			HttpSession session, 
			Model model) {
		
		// 校验验证码
		if (BaseVerCode.checkVerCode(vercode, session, model) == false) {
			model.addAttribute("question", question);
			question.setKiss(null);
			return "/jie/add";
		}
		
		User user = (User) session.getAttribute("user");
		question.setUid(user.getId());
		
		// 调用 service业务层
		int res = questionService.add(question);
		if (res > 0) {
			// 发表成功
			// 跳转到该帖子的详情页面
			return "redirect:/question/detail?id="+question.getId(); // 传递当前的帖子是哪一个帖子
			
			
		} else if (res == -1) {
			// 飞吻不足
			model.addAttribute("error", "您的飞吻不足, 请充值!!");
		} else {
			model.addAttribute("error", "未知错误, 请联系管理员");
		}
		
		return "/jie/add";
	}
	
	/**
	 * 帖子的详情页面
	 */
	@GetMapping("/detail")
	public String detail(Integer id, HttpSession session, Model model) {
		// 获取帖子的信息
		Question question = questionService.getById(id);
		
		// 传递到模版中
		model.addAttribute("question", question);
		
		// 获取帖子作者的信息
		User user = userService.getById(question.getUid());
		
		// 传递到模版中
		model.addAttribute("user", user);
		
		// 获取当前登录的用户
		User loginUser = (User) session.getAttribute("user");
		Integer uid = -1;
		if (loginUser != null) {
			uid = loginUser.getId();
		}
		
		// 获取帖子的回复信息
		List answers = answerService.getByQid(id, uid);
		
		// 传递到模版中
		model.addAttribute("answers", answers);
		
		
		// 计算帖子浏览的次数
		questionService.setViewsnum(question, uid);
		
		return "/jie/detail";
	}
	
	/*
	 * 回复的操作
	 */
	@PostMapping("/reply")
	@ResponseBody
	public Object reply(Answer answer, HttpSession session) {
		// 登录用户
		User user = (User) session.getAttribute("user");
		answer.setUid(user.getId());
		
		// 调用Service层进行处理
		int res = answerService.add(answer);
		
		Map<String, Object> result = new HashMap<>();
		
		if (res > 0) {
			// 插入成功
			result.put("status", 0);
			result.put("msg", "回复成功");
			result.put("user", user);
		} else {
			// 插入失败
			result.put("status", -1);
			result.put("msg", "回复失败");
		}
		
		return result;
	}
	
	/**
	 * 编辑帖子
	 * @param id
	 * @param model
	 * @return
	 */
	@GetMapping("/edit")
	public String edit(Integer id, Model model) {
		
		// 获取帖子的信息
		Question question = questionService.getById(id);
		
		// 传递到模版中
		model.addAttribute("question", question);
		
		return "/jie/add";
	}
	
	/**
	 * 编辑
	 * @param question
	 * @param vercode
	 * @param session
	 * @param model
	 * @return
	 */
	@PostMapping("/edit")
	public String edit(Question question, String vercode,
			HttpSession session, 
			Model model) {
		
		// 校验验证码
		if (BaseVerCode.checkVerCode(vercode, session, model) == false) {
			model.addAttribute("question", question);
			return "/jie/add";
		}
		
		User user = (User) session.getAttribute("user");
		question.setUid(user.getId());
		
		// 调用 service业务层
		int res = questionService.edit(question);
		if (res > 0) {
			// 发表成功
			// 跳转到该帖子的详情页面
			return "redirect:/question/detail?id="+question.getId(); // 传递当前的帖子是哪一个帖子
			
			
		} else if (res == -1) {
			// 飞吻不足
			model.addAttribute("error", "您的飞吻不足, 请充值!!");
		} else {
			model.addAttribute("error", "未知错误, 请联系管理员");
		}
		
		return "/jie/add";
	}
}





